<?php 
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;

class Comment extends controller
{	
	private $uid;
	public function _initialize()
	{
		( empty(cookie::get('loginid')) && empty(session::get('user')) )? $uid = '' : 
			(!empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid'));
		$this->uid = $uid;
	}

	public function index()
	{	
		$res = db('comment')->where('uid','=',$this->uid)->select();
		var_dump($res); die;
	}

	public function create(Request $request)
	{	
		$data = $request->post();
		$uid = $this->uid;

		if (empty($uid)) {
			$info['status'] = false;
	        $info['info'] = '亲,请先登录哦';
		}else{
			$data['uid'] = $uid;
			$data['ctime'] = date('Y-m-d h:i:s');
			$res = db('comment')->insert($data); 

			if($res){
	            $info['status'] = true;
	            $info['info'] = '发表成功';
	        }else{
	            $info['status'] = false;
	            $info['info'] = '阿偶出错了,过会再发吧';
	        }
		}
		
        return json($info);
	}

	public function delete(Request $request)
	{	
		$data = $request->post();

		$uid = $this->uid;
		if (!empty($data['uid']) && $data['uid'] == $uid) {
			$res = db('comment')->delete($data['id']); 

			if($res){
	            $info['status'] = true;
	            $info['info'] = '删除成功';
	        }else{
	            $info['status'] = false;
	            $info['info'] = '阿偶出错了';
	        }
	    }else{
        	$info['status'] = false;
            $info['info'] = '只能删除自己的评论哦';
	    }

        return json($info);
    }



}















